//
//  LTLNewCharacteristics.m
//  WeiBo
//
//  Created by LiTaiLiang on 16/9/27.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLNewCharacteristics.h"
#import "LTLTabBarController.h"
#import "LTLWelcome.h"

#define NewCharacteristicsImage 4

@interface LTLNewCharacteristics ()<UIScrollViewDelegate>

/**
 *  视图
 */
@property(nonatomic , weak) UIScrollView *Characteristics;
/**
 *  页面指示器
 */
@property(nonatomic , strong) UIPageControl * page;
///定时器
@property(nonatomic,strong) NSTimer * timer;
@end

@implementation LTLNewCharacteristics

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建视图
    UIScrollView *Characteristics = [[UIScrollView alloc]init];
    
    Characteristics.frame = self.view.frame;
    //设置代理
    Characteristics.delegate = self;
    
    //创建页面指示器并初始化
    UIPageControl *page = [[UIPageControl alloc]init];
    
    self.page = page;
    
    self.Characteristics = Characteristics;
    
    //添加视图
    [self.view  addSubview:Characteristics];
    
    [self didImage];
    
    [self.view addSubview:page];
    /**
     *  添加定时
     */
    [self addTimer:nil];
    
}
#pragma mark - 设置Characteristics并添加图片
-(void)didImage
{
    //内容大小
    self.Characteristics.contentSize = CGSizeMake( NewCharacteristicsImage * self.Characteristics.width , 0);
    //self.Characteristics.contentSize = CGSizeMake( 6 * self.Characteristics.width , 0);
    //开启分页
    self.Characteristics.pagingEnabled = YES;
    //隐藏指示器
    self.Characteristics.showsHorizontalScrollIndicator = NO;
    //关闭弹簧效果
    self.Characteristics.bounces = NO;
    /**
     *  添加图片
     */
    //    NSString *name =  [NSString stringWithFormat:@"IMG_20151129_112903"];
    //
    //    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:name]];
    //
    //    image.frame = self.view.frame;
    //
    //    image.x = 0* image.width;
    //    //图片不拉伸
    //    image.contentMode =  UIViewContentModeScaleAspectFill;
    //    [self.Characteristics addSubview:image];
    //    NSString *name2 =  [NSString stringWithFormat:@"IMG_20160928_104214"];
    //
    //    UIImageView *image2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:name2]];
    //
    //    image2.frame = self.view.frame;
    //
    //    image2.x = 1* image2.width;
    //    image2.contentMode =  UIViewContentModeScaleAspectFit;
    //    [self.Characteristics addSubview:image2];
    
    for (int i = 1; i <= NewCharacteristicsImage; i++) {
        
        NSString *name =  [NSString stringWithFormat:@"new_feature_%d", i];
        
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:name]];
        
        image.frame = self.view.frame;
        
        // image.x = (i+1) * image.width;
        image.x = (i-1) * image.width;
        
        [self.Characteristics addSubview:image];
        
        if (i == NewCharacteristicsImage) {
            [self DIDKonaJian:image];
        }
    }
    
    self.page.centerX = self.Characteristics.centerX;
    
    self.page.centerY = self.view.highly-60;
    //指示器页码
    self.page.numberOfPages = NewCharacteristicsImage;
    //    self.page.numberOfPages = 6;
    //设置当前页指示器颜色
    self.page.currentPageIndicatorTintColor = [UIColor orangeColor];
    //其他业指示器颜色
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    
}
///当轮播器滚动就会调用的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    ///根据轮播器的滚动位置决定图片显示第几页
    CGFloat scrollW=scrollView.width;
    int page=(scrollView.contentOffset.x+scrollW*0.5)/scrollW;
    self.page.currentPage=page;
}

-(void)DIDKonaJian:(UIImageView *)image
{
    image.userInteractionEnabled = YES;
    
    UIButton *button = [[UIButton alloc]init];
    
    NSString *selected = @"selected";
    BOOL isSelected = [[NSUserDefaults standardUserDefaults] boolForKey:selected];
    
    button.selected = !isSelected ;
    //不同状态的图片
    [button setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    
    [button setTitle:@"显示新特性" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(NewCharacteristics:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *jinRu = [[UIButton alloc]initText:@"体验微博" image:@"new_feature_finish_button" colorText:[UIColor whiteColor]];
    
    [jinRu addTarget:self action:@selector(winRootview) forControlEvents:UIControlEventTouchUpInside];
    
    [image addSubview:button];
    [image addSubview:jinRu];
    //布局
    [jinRu mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(image.mas_bottom).offset(-150);
        make.centerX.equalTo(image.mas_centerX);
        
    }];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(jinRu.mas_top).offset(-80);
        make.centerX.equalTo(jinRu.mas_centerX);
    }];
    
}

-(void)NewCharacteristics:(UIButton *)button
{
    button.selected = !button.isSelected;
    
    NSString *selected = @"selected";
    
    // 将当前状态存进沙盒
    [[NSUserDefaults standardUserDefaults] setBool:!button.isSelected forKey:selected];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
}

-(void)winRootview
{
    //切换根控制器
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    LTLWelcome *taber = [[LTLWelcome alloc]init];
    
    win.rootViewController = taber;
    
}
#pragma mark - 定时器轮播图片
///定时器
-(void)addTimer:(NSSet *)objects
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(nexImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}
///移除定时器
-(void)removeTimer:(NSSet *)objects
{
    [self.timer invalidate];
    self.timer = nil;
}
///定时器调用方法
-(void)nexImage
{
    //页数
    NSInteger count = 4;
    //当前页数
    NSInteger page = 0;
    
    if (self.page.currentPage == count-1)
    {
        
        [self removeTimer:nil];
        return;
    }
    else
    {
        page = self.page.currentPage+1;
    }
    //设置中心
    CGFloat offsetX = page*self.Characteristics.frame.size.width;
    CGPoint offset=CGPointMake(offsetX, 0);
    //偏移
    [self.Characteristics setContentOffset:offset animated:YES];
}

//用手滑动时移除定时器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer:nil];
}
//停止用手滑动时添加定时器
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addTimer:nil];
}


@end
